<script setup lang='ts'>
import type { messagesState } from '../type/messageState';
defineProps<{
  messages: messagesState[]
}>()

// 解析思考过程和答案
const formatThinkingContent = (content: string) => {
  return content.replace(/```thinking([\s\S]*?)```/g, '<div>$1</div>')
}
</script>


<template>
  <div class="context">
    <template v-for="(msg, index) in messages" :key="index">
      <div v-if="msg.role === 'user'" class="item_user">
        <div class="img_user">
          <img src="../../../assets/images/img1.png" alt="">
        </div>
        <div class="msg_content">
          {{ msg.content }}
        </div>
      </div>
      <div v-else class="item_ai">
        <div class="img_ai">
          <img src="../../../assets/images/img2.png" alt="">
        </div>
        <div class="msg_content">
          <!-- 加载或显示答案 -->
          <template v-if="msg.isLoading">
            <div v-loading="msg.isLoading"></div>
          </template>
          <template v-else>
            <div class="ai_response" v-html="formatThinkingContent(msg.content)"></div>
          </template>
        </div>
      </div>
    </template>
  </div>
</template>





<style scoped lang='scss'>
.context {

  .item_ai,
  .item_user {
    display: flex;
    align-items: flex-start;
    margin-bottom: 30px;
    min-height: 50px;

    .msg_content {
      display: flex;
      min-height: 30px;
      align-items: center;
      padding: 8px 12px;
      word-break: break-word;
      white-space: pre-wrap;
      border-radius: 8px;
    }
  }

  .item_ai {

    .img_ai {
      margin-right: 5px;

      img {
        height: 50px;
        width: 50px;
        border-radius: 50%;
      }
    }

    .msg_content {
      min-width: 40px;
      margin-right: 10px;
      background: #fff;
    }

    .ai_response {
      ::v-deep(think) {
        display: block;
        padding: 0 12px 12px;
        background: #f8f9fa;
        border-left: 3px solid #67c23a;
        color: #666;
        font-style: italic;
        border-radius: 4px;
        font-size: 0.95em;
        line-height: 1.6;
      }
    }
  }

  .item_user {
    // 反向排列
    flex-direction: row-reverse;

    .img_user {
      margin-left: 5px;

      img {
        height: 50px;
        width: 50px;
        border-radius: 50%;
      }
    }

    .msg_content {
      margin-left: 10px;
      flex-direction: row-reverse;
      background: #409eff;
      color: white;
    }
  }
}
</style>
